//
//  YGUility.m
//  YangGe
//
//  Created by EDZ on 2018/12/7.
//  Copyright © 2018 YG. All rights reserved.
//

#import "YGUility.h"

@implementation YGUility
+ (NSString *)timeWithTimeIntervalString:(NSString *)timeString andIsState:(NSInteger)state
{
    
    NSString *string = [NSString stringWithFormat:@"%@", timeString];
    if (string.length>9) {
        NSString *str = [string substringToIndex:10];
        NSTimeInterval time=[str doubleValue];
        NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time];
        //实例化一个NSDateFormatter对象
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        //设定时间格式,这里可以设置成自己需要的格式
        [dateFormatter setDateFormat:@"yyyy年MM月dd日 HH:mm"];
        if (state == 2) {
            
            [dateFormatter setDateFormat:@"MM月dd日 HH:mm"];
        }else if(state == 3){
            [dateFormatter setDateFormat:@"yyyy.MM.dd"];
            
        }else if(state == 4){
            [dateFormatter setDateFormat:@"yyyy/MM/dd"];
            
        }else if(state == 5){
            [dateFormatter setDateFormat:@"YYYY-MM"];
            
        }else if (state == 6){
            
            [dateFormatter setDateFormat:@"MM-dd HH:mm"];
        }else if (state == 7){
            
            [dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm"];
            
        }else if (state == 8){
            
            [dateFormatter setDateFormat:@"HH:mm"];
            
        }else if (state == 9){
            
            [dateFormatter setDateFormat:@"YYYY-MM-dd"];
            
        }else if (state == 10){
            
            [dateFormatter setDateFormat:@"HH'mm\""];
            
        }else if (state == 11){
            
            [dateFormatter setDateFormat:@"dd天HH小时mm分钟"];
            
        }
        
        
        NSString *currentDateStr = [dateFormatter stringFromDate: detaildate];
        return currentDateStr;
    }else{
        
        // 格式化时间
        NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
        //        formatter.timeZone = [NSTimeZone timeZoneWithName:@"shanghai"];
        [formatter setDateStyle:NSDateFormatterMediumStyle];
        [formatter setTimeStyle:NSDateFormatterShortStyle];
        [formatter setDateFormat:@"yyyy年MM月dd日 HH:mm"];
        //        [formatter setDateFormat:@"dd天HH小时mm分钟"];
        
        // 毫秒值转化为秒
        NSDate* date = [NSDate dateWithTimeIntervalSince1970:[timeString doubleValue]/ 1000.0];
        NSString* dateString = [formatter stringFromDate:date];
        return dateString;
    }
}

+ (NSString *)timeWithTimeIntervalStringNew:(NSString *)timeString{
    
    // 格式化时间
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    //        formatter.timeZone = [NSTimeZone timeZoneWithName:@"shanghai"];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    //    [formatter setDateFormat:@"yyyy年MM月dd日 HH:mm"];
    [formatter setDateFormat:@"dd天HH小时mm分钟"];
    
    // 毫秒值转化为秒
    NSDate* date = [NSDate dateWithTimeIntervalSince1970:[timeString doubleValue]];
    NSString* dateString = [formatter stringFromDate:date];
    return dateString;
    
    
}


+(UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize
{
    // Create a graphics image context
    UIGraphicsBeginImageContext(newSize);
    
    // Tell the old image to draw in this new context, with the desired
    // new size
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    
    // Get the new image from the context
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    // End the context
    UIGraphicsEndImageContext();
    
    // Return the new image.
    return newImage;
}


+(CGFloat)heighForCell: (NSString *)content andSize:(CGSize )size andFont:(CGFloat)fontHeight
{
    
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:fontHeight] forKey:NSFontAttributeName];
    
    CGRect frame =  [content boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    
    return frame.size.height;
    
}

+ (NSString *)checkTheDate:(NSString *)string{

NSDateFormatter *format = [[NSDateFormatter alloc]init];
[format setDateFormat:@"yyyy-MM-dd HH-mm-ss"];
//    [format setDateFormat:@"yyyy-MM-dd"];
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    [format setTimeZone:timeZone];
NSDate *date = [format dateFromString:string];
BOOL isToday = [[NSCalendar currentCalendar] isDateInToday:date];
    BOOL isToday2 = [[NSCalendar currentCalendar] isDateInYesterday:date];

NSString *strDiff = @"";

if(isToday) {
    strDiff= @"今天";
}
    if (isToday2) {
        strDiff= @"昨天";
    }
return strDiff;
}

+ (NSString*)weekdayStringFromDate:(NSString*)featureDate {
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

    formatter.dateFormat = @"yyyy-MM-dd HH-mm-ss";
    
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    [formatter setTimeZone:timeZone];
    
    NSDate *inputDate = [formatter dateFromString:featureDate];
    NSTimeInterval secondsPerDay = 24 * 60 * 60;
    NSDate *today = [[NSDate alloc] init];
    NSDate *tomorrow, *yesterday;
    
    tomorrow = [today dateByAddingTimeInterval: secondsPerDay];
    yesterday = [today dateByAddingTimeInterval: -secondsPerDay];
    
    NSString * todayString = [[today description] substringToIndex:10];
    NSString * yesterdayString = [[yesterday description] substringToIndex:10];
    NSString * dateString = [[inputDate description] substringToIndex:10];
 
    if ([dateString isEqualToString:todayString])
    {
        return @"今天";
    } else if ([dateString isEqualToString:yesterdayString])
    {
        return @"昨天";
    }
  
    
    return @"";
}


+(NSString*)getTimeStrWithString:(NSString*)str{ NSDateFormatter*dateFormatter=[[NSDateFormatter alloc]init];// 创建一个时间格式化对象
    [dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];//设定时间的格式
    NSDate *tempDate=[dateFormatter dateFromString:str];//将字符串转换为时间对象
    NSString*timeStr=[NSString stringWithFormat:@"%ld",(long)[tempDate timeIntervalSince1970]*1000];//字符串转成时间戳,精确到毫秒*1000
    return timeStr;
    
}



// 过去时间转化
+ (NSString *)timeIntervalDescriptionString:(NSString *)timeString
{
    
    timeString = [self getTimeStrWithString:timeString];
    
    // 格式化时间
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    //        formatter.timeZone = [NSTimeZone timeZoneWithName:@"shanghai"];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyy年MM月dd日 HH:mm"];
    
    // 毫秒值转化为秒
    NSDate* date = [NSDate dateWithTimeIntervalSince1970:[timeString doubleValue]/ 1000.0];
    //    NSString* dateString = [formatter stringFromDate:date];
    NSString* dateString = [date ss_timeIntervalDescription];
    return dateString;
}

+ (NSString *)gjString:(NSString *)str{
    NSString *string = @"";
    if ([str isKindOfClass:[NSString class]]) {
        
        string = str;
    }
    
    
    return string;
}


+ (UIImage *)imageWithColor:(UIColor *)color;
{
    @autoreleasepool {
        CGRect rect = CGRectMake(0, 0, 1, 1);
        UIGraphicsBeginImageContext(rect.size);
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSetFillColorWithColor(context, color.CGColor);
        CGContextFillRect(context, rect);
        UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return img;
    }
}



+ (NSString *)gjFloatString:(NSString *)string{
//    if (![string isKindOfClass:[NSString class]]) {
//    
//        return @"0";
//    }
        if (string.floatValue == 0) {
    
            return @"0";
        }
    float a = [[NSString stringWithFormat:@"%.2f",string.floatValue] floatValue];
    float b = [[NSString stringWithFormat:@"%.1f",string.floatValue] floatValue];
    float c = [[NSString stringWithFormat:@"%.f",string.floatValue] floatValue];
    //    NSLog(@"c == %f,b===%f,a==%f",c,b,a);
    if (c == a) {
        //.00情况
        return [NSString stringWithFormat:@"%.f",c];
    }else if (b == a) {
        //.0情况
        return [NSString stringWithFormat:@"%.1f",b];
    }else{
        return  [NSString stringWithFormat:@"%.2f",a];
    }
    
    
    
}


+ (NSString *)gjPriceString:(NSString *)string{
    
    if (![string isKindOfClass:[NSString class]]) {
        return @"0";
    }
    //      NSString *price = @"0";
    float p = string.floatValue;
    ////小于一千
    //    if (p < 1000) {
    //     return  [self gjFloatString:string];
    //    }
    //    //小于一万
    //    if (p < 10000) {
    //        return  [NSString stringWithFormat:@"%@k",[self gjFloatString:[NSString stringWithFormat:@"%f",(p/1000)]]];
    //    }
    ////    其他的
    //    return  [NSString stringWithFormat:@"%@w",[self gjFloatString:[NSString stringWithFormat:@"%f",(p/10000)]]];
    
    //小于一万
    if (p < 10000) {
        return  [self gjFloatString:string];
    }
    
    return  [NSString stringWithFormat:@"%@万",[self gjFloatString:[NSString stringWithFormat:@"%f",(p/10000)]]];
    
    
    //    return price;
}

+ (NSString *)gjFollowString:(NSInteger )number{
    
    NSString *num = [NSString stringWithFormat:@"%zd",number];
    float p = num.floatValue;
    //小于一万
    if (number < 10000) {
        return  num;
    }
    //大于一万
    if (number >= 10000) {
        return  [NSString stringWithFormat:@"%@k",[self gjFloatString:[NSString stringWithFormat:@"%f",(p/1000)]]];
    }
//    //小于10万
//    if (number < 100000) {
//        return  [NSString stringWithFormat:@"%@w",[self gjFloatString:[NSString stringWithFormat:@"%f",(p/10000)]]];
//    }
//    //其他的
//    //    return  [NSString stringWithFormat:@"%@w",[self gjFloatString:[NSString stringWithFormat:@"%zd",(number/10000)]]];
//    if (number >= 100000) {
//        //        return @"10w+";
//        return @"10w+";
//    }
    
    return @"0";
}

+ (CGFloat)getLabelHeightWithText:(NSString *)text width:(CGFloat)width font: (CGFloat)font
{
    CGRect rect = [text boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:font]} context:nil];
    
    return rect.size.height;
}
//根据高度度求宽度  text 计算的内容  Height 计算的高度 font字体大小
+ (CGFloat)getWidthWithText:(NSString *)text height:(CGFloat)height font:(CGFloat)font{
    
    CGRect rect = [text boundingRectWithSize:CGSizeMake(MAXFLOAT, height)
                                     options:NSStringDrawingUsesLineFragmentOrigin
                                  attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:font]}
                                     context:nil];
    return rect.size.width;
    
}

/**
 view 传入要变圆角的视图
 size自己根据需要设置角度大小
 后面的4个角 BOOL 1 是设置该角为圆角 0 不改变
 */
+ (void)renYiYuanJiao:(UIView *)view size:(CGSize)size left:(BOOL)left right:(BOOL)right bottomLeft:(BOOL)bottomLeft bottomRight:(BOOL)bottomRight {
    UIRectCorner Left = 0;
    UIRectCorner Right = 0;
    UIRectCorner BottomLeft = 0;
    UIRectCorner BottomRight = 0;
    if (left) {
        Left = UIRectCornerTopLeft;
    }
    if (right) {
        Right = UIRectCornerTopRight;;
    }
    if (bottomLeft) {
        BottomLeft = UIRectCornerBottomLeft;
    }
    if (bottomRight) {
        BottomRight = UIRectCornerBottomRight;
    }
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:Left|Right|BottomLeft|BottomRight cornerRadii:size];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = view.bounds;
    maskLayer.path = maskPath.CGPath;
    view.layer.mask = maskLayer;
}

/**
 将UTC日期字符串转为本地时间字符串
 eg: 2017-10-25 02:07:39  -> 2017-10-25 10:07:39
 */
+ (NSString *)getLocalDateFormateUTCDate:(NSString *)utcStr {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //输入格式
    [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
    NSTimeZone *localTimeZone = [NSTimeZone localTimeZone];
    [dateFormatter setTimeZone:localTimeZone];  
    
    NSDate *dateFormatted = [dateFormatter dateFromString:utcStr];
    //输出格式
//    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSString *dateString = [dateFormatter stringFromDate:dateFormatted];

    return dateString;
}

//去除空格和换行
+ (NSString *)removeSpaceAndNewline:(NSString *)str{
    
    NSString *temp = [str stringByReplacingOccurrencesOfString:@" " withString:@""];
    temp = [temp stringByReplacingOccurrencesOfString:@"\r" withString:@""];
    temp = [temp stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    return temp;
    
}

+ (BOOL)stringContainsEmoji:(NSString *)string
{
    __block BOOL returnValue = NO;
    [string enumerateSubstringsInRange:NSMakeRange(0, [string length])
                               options:NSStringEnumerationByComposedCharacterSequences usingBlock:
     ^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
         const unichar hs = [substring characterAtIndex:0];
         // surrogate pair
         if (0xd800 <= hs && hs <= 0xdbff) {
             if (substring.length > 1) {
                 const unichar ls = [substring characterAtIndex:1];
                 const int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
                 if (0x1d000 <= uc && uc <= 0x1f77f) {
                     returnValue = YES;
                 }
             }
         } else if (substring.length > 1) {
             const unichar ls = [substring characterAtIndex:1];
             if (ls == 0x20e3) {
                 returnValue = YES;
             }
         } else {
             // non surrogate
             if (0x2100 <= hs && hs <= 0x27ff) {
                 returnValue = YES;
             } else if (0x2B05 <= hs && hs <= 0x2b07) {
                 returnValue = YES;
             } else if (0x2934 <= hs && hs <= 0x2935) {
                 returnValue = YES;
             } else if (0x3297 <= hs && hs <= 0x3299) {
                 returnValue = YES;
             } else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50) {
                 returnValue = YES;
             }
         }
     }];
    return returnValue;
}


+(BOOL)judgePassWordLegal:(NSString *)pass{
    BOOL result = false;
    if ([pass length] >= 6 && [pass length] <= 18){
        // 判断长度大于8位后再接着判断是否同时包含数字和字符
        NSString * regex = @"^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,18}$";
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
        result = [pred evaluateWithObject:pass];
    }
    return result;
}

+ (void)tel {
    [BaseToastView showToast:@"没电话号码"];
    return;
    NSMutableString *str=[[NSMutableString alloc]initWithFormat:@"tel:%@",@""];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
    
}

+ (void)tel:(NSString *)phone {

    if ([self gjString:phone].length == 0) {
        [BaseToastView showToast:@"电话号码为空,无法拨打!"];
         return;
    }
   
    NSMutableString *str=[[NSMutableString alloc]initWithFormat:@"tel:%@",phone];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
    
}
//金额转换成大写
+ (NSString *)getAmountInWords:(NSString *)money{
    if (money.length == 0) {
        return @"";
    }
    if (money.floatValue == 0) {
        return @"零圆整";
    }
    //大写数字
    NSArray *upperArray = @[ @"零",@"壹",@"贰",@"叁",@"肆",@"伍",@"陆",@"柒",@"捌",@"玖" ];
    /** 整数部分的单位 */
    NSArray *measureArray = @[ @"", @"拾", @"佰", @"仟"];
    /** 整数部分的单位 */
    NSArray *intUnit = @[@"圆", @"万", @"亿"];
    /** 小数部分的单位 */
    NSArray *floatUnitArray = @[ @"角", @"分" ];
    
    NSString *upIntNum = [NSString string];
    NSString *upFloatNum = [NSString string];
    NSArray *numArray = [money componentsSeparatedByString:@"."];
    
    NSString *str1 = [numArray objectAtIndex:0];
    NSInteger num1 = str1.integerValue;
    for (int i = 0; i < intUnit.count && num1 > 0; i++) {//这一部分就是单纯的转化
        NSString *temp = @"";
        int tempNum = num1%10000;
        if (tempNum != 0 || i == 0) {
            for (int j = 0; j < measureArray.count && num1 > 0; j++) {
                temp = [NSString stringWithFormat:@"%@%@%@", [upperArray objectAtIndex:num1%10], [measureArray objectAtIndex:j],temp];//每次转化最后一位数
                num1 = num1/10;//数字除以10
            }
            upIntNum = [[temp stringByAppendingString:[intUnit objectAtIndex:i]] stringByAppendingString:upIntNum];
        } else {
            num1 /= 10000;
            temp = @"零";
            upIntNum = [temp stringByAppendingString:upIntNum];
        }
        
    }
    
    for (int m = 1; m < measureArray.count; m++) { //把零佰零仟这种情况转为零
        NSString *lingUnit = [@"零" stringByAppendingString:[measureArray objectAtIndex:m]];
        upIntNum = [upIntNum stringByReplacingOccurrencesOfString:lingUnit withString:@"零"];
    }
    
    while ([upIntNum rangeOfString:@"零零"].location != NSNotFound) {//多个零相邻的保留一个零
        upIntNum = [upIntNum stringByReplacingOccurrencesOfString:@"零零" withString:@"零"];
    }
    for (int k = 0; k < intUnit.count * 2; k++) { //零万、零亿这种情况转化为万零
        NSString *unit = [intUnit objectAtIndex:k%intUnit.count];
        NSString *lingUnit = [@"零" stringByAppendingString:unit];
        upIntNum = [upIntNum stringByReplacingOccurrencesOfString:lingUnit withString:[unit stringByAppendingString:@"零"]];
    }
    
    if (numArray.count == 2) {//小数部分转化
        NSString *floatStr = [numArray objectAtIndex:1];
        for (NSInteger i = floatStr.length; i > 0; i--) {
            NSString *temp = [floatStr substringWithRange:NSMakeRange(floatStr.length - i, 1)];
            NSInteger tempNum = temp.integerValue;
            if (tempNum == 0) continue;
            NSString *upNum = [upperArray objectAtIndex:tempNum];
            NSString *unit = [floatUnitArray objectAtIndex:floatStr.length - i];
            if (i < floatStr.length && upFloatNum.length == 0 && upIntNum.length > 0) {
                upFloatNum = @"零";
            }
            upFloatNum = [NSString stringWithFormat:@"%@%@%@", upFloatNum, upNum, unit];
        }
    }
    if (upFloatNum.length == 0) {
        upFloatNum = @"整";
    }
    
    NSString *amountInWords = [NSString stringWithFormat:@"%@%@", upIntNum, upFloatNum];
    
    while ([amountInWords rangeOfString:@"零零"].location != NSNotFound) {//再次除去多余的零
        amountInWords = [amountInWords stringByReplacingOccurrencesOfString:@"零零" withString:@"零"];
    }
    
    if ([amountInWords rangeOfString:@"零整"].location != NSNotFound) {
        amountInWords = [amountInWords stringByReplacingOccurrencesOfString:@"零整" withString:@"整"];
    }
    
    return amountInWords;
    
}

+(NSInteger)timeSwitchTimestamp:(NSString *)formatTime andFormatter:(NSString *)format{
    
    
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    
    [formatter setDateFormat:format]; //(@"YYYY-MM-dd hh:mm:ss") ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    
    
    
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    
    [formatter setTimeZone:timeZone];
    
    
    
    NSDate* date = [formatter dateFromString:formatTime]; //------------将字符串按formatter转成nsdate
    
    //时间转时间戳的方法:
    
    NSInteger timeSp = [[NSNumber numberWithDouble:[date timeIntervalSince1970]] integerValue];
    
    
    
    NSLog(@"将某个时间转化成 时间戳&&&&&&&timeSp:%ld",(long)timeSp); //时间戳的值
    
    
    
    return timeSp;
    
}

+ (NSString *)dataZhuanHuan:(NSString *)timeString{
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    
    NSDate *currentDate = [dateFormatter dateFromString:timeString];
    
    
    
    NSDateFormatter *outputFormatter = [[NSDateFormatter alloc]init];
    
    [outputFormatter setDateFormat:@"yyyy年MM月dd日"];
    
    NSString *timeChanged = [outputFormatter stringFromDate:currentDate];
    return timeChanged;
}

//获取当前时间
+ (NSString *)getCurrentTime:(NSInteger)type {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    if (type == 1) {
        [formatter setDateFormat:@"MM-dd"];
    }else if (type == 0){
      [formatter setDateFormat:@"yyyy-MM-dd"];
    }else  if (type == 2){
      [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    }
    
    NSString *dateTime = [formatter stringFromDate:[NSDate date]];
    return dateTime;
}


//传入今天的时间，返回明天的时间
+ (NSString *)GetTomorrowDay:(NSDate *)aDate {
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *components = [gregorian components:NSCalendarUnitWeekday | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:aDate];
    [components setDay:([components day]+1)];
    
    NSDate *beginningOfWeek = [gregorian dateFromComponents:components];
    NSDateFormatter *dateday = [[NSDateFormatter alloc] init];
    [dateday setDateFormat:@"yyyy-MM-dd"];
    return [dateday stringFromDate:beginningOfWeek];
}

// 字典转json字符串方法

+(NSString *)convertToJsonData:(NSDictionary *)dict{
    
    NSError *error;
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
    
    NSString *jsonString;
    
    if (!jsonData) {
        
        NSLog(@"%@",error);
        
    }else{
        
        jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
        
    }
    
    NSMutableString *mutStr = [NSMutableString stringWithString:jsonString];
    
    NSRange range = {0,jsonString.length};
    
    //去掉字符串中的空格
    
    [mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range];
    
    NSRange range2 = {0,mutStr.length};
    
    //去掉字符串中的换行符
    
    [mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range2];
    
    return mutStr;
    
}
+ (NSString *)jsonStringWithDict:(id )dict {
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
    
    NSString *jsonString;
    if (!jsonData) {
        NSLog(@"%@",error);
    }else{
        jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
    }
    
    NSMutableString *mutStr = [NSMutableString stringWithString:jsonString];
    NSRange range = {0,jsonString.length};
    //去掉字符串中的空格
    [mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range];
    NSRange range2 = {0,mutStr.length};
    //去掉字符串中的换行符
    [mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range2];
    return mutStr;
}


+ (CGFloat)hideLabelLayoutHeight:(NSMutableAttributedString *)content withTextFontSize:(CGFloat)mFontSize andWidth:(CGFloat)width
{
    if (content.length == 0) {
        return 0;
    }
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    
//    paragraphStyle.lineSpacing = 5;  // 段落高度
    
//    NSMutableAttributedString *attributes = content;
    
    [content addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:mFontSize] range:NSMakeRange(0, content.length)];
    [content addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, content.length)];
    
    CGSize attSize = [content boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil].size;
    
    return attSize.height;
}
+ (CGFloat)hideLabelLayoutWidth:(NSMutableAttributedString *)content withTextFontSize:(CGFloat)mFontSize andWidth:(CGFloat)height{
    
    if (content.length == 0) {
        return 0;
    }
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];

    [content addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:mFontSize] range:NSMakeRange(0, content.length)];
    [content addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, content.length)];
    
    CGSize attSize = [content boundingRectWithSize:CGSizeMake(MAXFLOAT, height) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil].size;
    
    return attSize.width;
    
}


+ (CGFloat)textHeightWithString:(NSString*)string Font:(UIFont*)font maxWidth:(CGFloat)width{
        CGFloat height = 0;
        do{
                if(string.length<=0) {
                        break;
                    }
                //字符串中的空格会影响换行的高度，填充空格后再计算
                NSString *allStr = [string stringByReplacingOccurrencesOfString:@" " withString:@"a"];
                NSDictionary *attribute = @{NSFontAttributeName:font};
                CGRect retSize = [allStr boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingTruncatesLastVisibleLine |
                                                            NSStringDrawingUsesLineFragmentOrigin |
                                                            NSStringDrawingUsesFontLeading attributes:attribute context:nil];
                height = retSize.size.height;
            }while(0);
       return ceil(height);
}

+ (UIImage *)fixOrientation:(UIImage *)aImage {
    
    // No-op if the orientation is already correct
    if (aImage.imageOrientation == UIImageOrientationUp)
        return aImage;
    
    // We need to calculate the proper transformation to make the image upright.
    // We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored.
    CGAffineTransform transform = CGAffineTransformIdentity;
    
    switch (aImage.imageOrientation) {
        case UIImageOrientationDown:
        case UIImageOrientationDownMirrored:
            transform = CGAffineTransformTranslate(transform, aImage.size.width, aImage.size.height);
            transform = CGAffineTransformRotate(transform, M_PI);
            break;
            
        case UIImageOrientationLeft:
        case UIImageOrientationLeftMirrored:
            transform = CGAffineTransformTranslate(transform, aImage.size.width, 0);
            transform = CGAffineTransformRotate(transform, M_PI_2);
            break;
            
        case UIImageOrientationRight:
        case UIImageOrientationRightMirrored:
            transform = CGAffineTransformTranslate(transform, 0, aImage.size.height);
            transform = CGAffineTransformRotate(transform, -M_PI_2);
            break;
        default:
            break;
    }
    
    switch (aImage.imageOrientation) {
        case UIImageOrientationUpMirrored:
        case UIImageOrientationDownMirrored:
            transform = CGAffineTransformTranslate(transform, aImage.size.width, 0);
            transform = CGAffineTransformScale(transform, -1, 1);
            break;
            
        case UIImageOrientationLeftMirrored:
        case UIImageOrientationRightMirrored:
            transform = CGAffineTransformTranslate(transform, aImage.size.height, 0);
            transform = CGAffineTransformScale(transform, -1, 1);
            break;
        default:
            break;
    }
    
    // Now we draw the underlying CGImage into a new context, applying the transform
    // calculated above.
    CGContextRef ctx = CGBitmapContextCreate(NULL, aImage.size.width, aImage.size.height,
                                             CGImageGetBitsPerComponent(aImage.CGImage), 0,
                                             CGImageGetColorSpace(aImage.CGImage),
                                             CGImageGetBitmapInfo(aImage.CGImage));
    CGContextConcatCTM(ctx, transform);
    switch (aImage.imageOrientation) {
        case UIImageOrientationLeft:
        case UIImageOrientationLeftMirrored:
        case UIImageOrientationRight:
        case UIImageOrientationRightMirrored:
            // Grr...
            CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.height,aImage.size.width), aImage.CGImage);
            break;
            
        default:
            CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.width,aImage.size.height), aImage.CGImage);
            break;
    }
    
    // And now we just create a new UIImage from the drawing context
    CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
    UIImage *img = [UIImage imageWithCGImage:cgimg];
    CGContextRelease(ctx);
    CGImageRelease(cgimg);
    return img;
}


//获取当前的时间
+(NSString*)getCurrentTimes{
     NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
     // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
     [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
     //现在时间,你可以输出来看下是什么格式
     NSDate *datenow = [NSDate date];
     //----------将nsdate按formatter格式转成nsstring
     NSString *currentTimeString = [formatter stringFromDate:datenow];
     NSLog(@"currentTimeString = %@",currentTimeString);
     return currentTimeString;
}

+ (void)checkVideoAuthorization {
     //是否有摄像头权限
    AVAuthorizationStatus statusVideo = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if (statusVideo == AVAuthorizationStatusDenied) {
        [BaseToastView showToastWithText:@"获取摄像头权限失败，请前往隐私-相机设置里面打开应用权限" Type:YLToastTypeShort ToView:nil  ];
    }else if(statusVideo == AVAuthorizationStatusNotDetermined){
        [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
         }];
    }
}

+ (void)homePageTap:(NSString *)userNo{
 
    
    
}

+ (void)toLogin{
//    if (![GETCURRENTCONTROLLER isKindOfClass:[RELoginViewController class]]) {
//        SetToken(@"");
//        SetLogInState(NO);
//        RELoginViewController *vc = [RELoginViewController new];
//        vc.hidesBottomBarWhenPushed = YES;
//        [GETCURRENTCONTROLLER.navigationController pushViewController:vc animated:YES];
//    }
    
}

+ (NSDate *)getDatetampFromTime:(NSString *)dateStr{

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

    [formatter setDateStyle:NSDateFormatterMediumStyle];

    [formatter setTimeStyle:NSDateFormatterShortStyle];

    [formatter setDateFormat:@"yyyy-MM-dd"];

    //设置时区,这个对于时间的处理有时很重要


    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];

    [formatter setTimeZone:timeZone];

    NSDate *date = [formatter dateFromString:dateStr];
//    NSString *nowtimeStr = [formatter stringFromDate:dateStr];//----------将nsdate按formatter格式转成nsstring

//    NSLog(@"%@", nowtimeStr);

    // 时间转时间戳的方法:

//    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]];

    if (!date) {
        date = [NSDate date];
    }
    return date;

}



@end
